package com.zx.deploy.controller;

import com.zx.deploy.common.utils.FileUtil;
import com.zx.deploy.common.utils.ReturnModel;
import com.zx.deploy.config.DeployConfig;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by IBM on 2018/3/30.
 */
@RequestMapping("/log")
@Controller
public class LogController {

    @Resource
    private DeployConfig deployConfig;

    @GetMapping("/index")
    public String index(){
        return "log";
    }

    @ResponseBody
    @GetMapping("/list")
    public Object list(){
        try {
            return ReturnModel.success("成功", FileUtil.listFiles(deployConfig.tomat_path + deployConfig.tomcat_logs,""));
        } catch (Exception e) {
            e.printStackTrace();
            return ReturnModel.error("查询数据备份列表失败");
        }
    }


    @GetMapping("/download")
    public void download(String filename, HttpServletResponse response) throws IOException {
        response.setContentType("application/force-download");
        response.setHeader("Content-Disposition","attchment;filename="+filename);
        OutputStream out = response.getOutputStream();
        FileUtils.copyFile(FileUtils.getFile(deployConfig.tomat_path + deployConfig.tomcat_logs + "/" + filename),out);
        out.flush();
        out.close();
    }

}
